package com.github.m6d21.sso.service.permission.impl;

import com.github.m6d21.sso.core.exception.BusinessException;
import com.github.m6d21.sso.dao.permission.dao.IPermissionDao;
import com.github.m6d21.sso.model.permission.entity.Permission;
import com.github.m6d21.sso.model.permission.qo.PermissionQO;
import com.github.m6d21.sso.service.permission.IPermissionService;
import com.github.m6d21.sso.support.enums.StatusCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import org.springframework.stereotype.Service;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2019/1/4 Fri 17:19:00
 * @since 1.0.0
 */
@Service("permissionService")
public class PermissionServiceImpl implements IPermissionService {

    private static final int DEFAULT_PAGE_NO = 1;
    private static final int DEFAULT_PAGE_SIZE = 20;

    @Resource
    private IPermissionDao permissionDao;

    @Override
    public List<Permission> query(PermissionQO permissionQO) {
        return permissionDao.query(permissionQO);
    }

    @Override
    public PageInfo<Permission> queryByPage(Integer pageNo, Integer pageSize, PermissionQO permissionQO) {
        pageNo = (pageNo == null ? DEFAULT_PAGE_NO : pageNo);
        pageSize = (pageSize == null ? DEFAULT_PAGE_SIZE : pageSize);
        PageHelper.startPage(pageNo, pageSize);
        List<Permission> list = permissionDao.query(permissionQO);
        return PageInfo.of(list);
    }

    @Override
    public PageInfo<Permission> queryByPageForAdmin(Integer pageNo, Integer pageSize, PermissionQO permissionQO) {
        pageNo = (pageNo == null ? DEFAULT_PAGE_NO : pageNo);
        pageSize = (pageSize == null ? DEFAULT_PAGE_SIZE : pageSize);
        PageHelper.startPage(pageNo, pageSize);
        List<Permission> list = permissionDao.queryForAdmin(permissionQO);
        return PageInfo.of(list);
    }

    @Override
    public Permission savePermission(Permission permission) {
        boolean success = permissionDao.insertSelectivePermission(permission);
        if (!success) {
            throw new BusinessException(StatusCode.FAIL.getCode(), "保存权限失败");
        }
        return permission;
    }

    @Override
    public Permission modifyPermissionById(Permission permission) {
        boolean success = permissionDao.updateSelectivePermissionById(permission);
        if (!success) {
            throw new BusinessException(StatusCode.FAIL.getCode(), "更新权限失败");
        }
        return permission;
    }

    @Override
    public Boolean deletePermissionById(@NotNull(message = "权限id不能为空") Integer id) {
        return permissionDao.deleteById(id);
    }

    @Override
    public Boolean deletePermissionByIds(@NotNull(message = "权限ids不能为空") List<Integer> ids) {
        return permissionDao.deleteByIds(ids);
    }

    @Override
    public Boolean existByPermissionCode(@NotNull(message = "权限Code不能为空") String permissionCode) {
        PermissionQO permissionQO = new PermissionQO();
        permissionQO.setPermissionCode(permissionCode);
        List<Permission> list = permissionDao.find(permissionQO);
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }
}
